import { computed } from 'vue';
import { useUserStore } from '@/store/user';
import { whitePagelist } from '@/utils/constant';
import { redirectTo } from '@/utils/utils';

// 权限页和白名单集合
const permission = computed(() => {
  const userStore = useUserStore();
  const pages = userStore.getPages();
  return [...pages, ...whitePagelist];
});

export const hasPermission = (url: string): boolean => permission.value.includes(url);

export function gotoLogin() {
  const routes = getCurrentPages();
  const currentRoute = routes[routes.length - 1].route + '';
  let redirectUrl = '';
  if (currentRoute !== 'pages/login/login') {
    redirectUrl = `?redirect=${encodeURIComponent(currentRoute)}`;
    redirectTo(`pages/login/login${redirectUrl ?? ''}`);
  }
}
